<template>
  <div class="panel">
    <!-- 完成新增 & 修改 -->
    <el-dialog
      :title="form.id ? '编辑权限' : '新增权限'"
      :visible="showDialog"
      @close="cancle"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="企业可见">
          <!-- <el-input v-model="form.enVisible"></el-input> -->
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button @click="submit" type="primary">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPerDetailApi, updatePerApi, addPerApi } from '@/api/permission'
export default {
  // showDialog: 控制面板的显示与隐藏
  props: ['showDialog'],
  data () {
    return {
      // 数据源
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
    }
  },
  methods: {
    // 得到 type & pid 的值
    getData (type, pid) {
      this.form.type = type
      this.form.pid = pid
    },
    // 根据 id 得到权限详情信息
    async getPerDetail (id) {
      const res = await getPerDetailApi(id)
      this.form = res.data
    },
    cancle () {
      // 关闭面板
      this.$emit('update:showDialog', false)
      // 清空数据
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
    },
    // 提示数据
    async submit () {
      if (this.form.id) {
        // 提示修改数据
        await updatePerApi(this.form)
      } else {
        // 提交新增的数据
        await addPerApi(this.form)
      }
      // 提示操作成功
      this.$message.success(this.form.id ? '修改成功' : '新增成功')
      // 关闭面板
      this.$emit('update:showDialog', false)
      // 更新数据
      this.$emit('updateData')
      // 清空数据
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: ''
      }
    }
  }
}
</script>

<style></style>
